package lyxs_916.core;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import lyxs_916.sample.R;

/**
 * Created by 阳 on 2016/10/10.
 */

public abstract class BaseActivity extends AppCompatActivity implements Toolbar.OnMenuItemClickListener {
    protected Toolbar toolbar;
    protected TextView toolbar_title;
    public static final int MODE_BACK = 0;
    public static final int MODE_NONE = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setUpContentView();
        setUpView();
        setUpData();
    }

    protected abstract void setUpContentView();

    protected abstract void setUpView();

    protected abstract void setUpData();

    @Override
    public void setContentView( int layoutResID) {
        setContentView(layoutResID, -1, -1, MODE_BACK);
    }

    public void setContentView( int layoutResID, int titltResId) {
        setContentView(layoutResID, -titltResId, -1, MODE_BACK);
    }

    public void setContentView( int layoutResID, int titltResId, int mode) {
        setContentView(layoutResID, titltResId, mode, MODE_BACK);
    }

    public void setContentView( int layoutResID, int titltResId, int mode, int menuId) {
        super.setContentView(layoutResID);
        setUpToolBar(titltResId, mode, menuId);
    }

    public void setUpToolBar(int titltResId, int mode, int menuId) {
        if (mode != MODE_NONE) {
            toolbar = (Toolbar) findViewById(R.id.toolbar);
            toolbar_title = (TextView) findViewById(R.id.toolbar_title);

            if (mode == MODE_BACK) {
                toolbar.setNavigationIcon(R.drawable.material_logo);
                toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
            }
            setUpTitle(titltResId);
            setUpMenu(menuId);
        }
    }

    public void setUpTitle(int titltResId) {
        if (titltResId > 0 && toolbar != null) {
            toolbar.setTitle(titltResId);
        }
    }

    public void setUpMenu(int menuId) {
        if (toolbar != null) {
            toolbar.getMenu().close();
            if (menuId > 0) {
                toolbar.inflateMenu(menuId);
                toolbar.setOnMenuItemClickListener(this);
            }
        }
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        return false;
    }

    @Override
    protected void onStart() {
        super.onStart();
    }


}
